package cn.bdqn.controller;

import cn.bdqn.domain.Teacher;
import cn.bdqn.service.TeacherService;
import cn.bdqn.utils.result.Page;
import cn.bdqn.utils.result.Result;
import cn.bdqn.utils.result.ResultGenerator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;


/**
 * 老师类控制层
 *
 * @author qq175
 */
@RestController
@RequestMapping("/teacher")
public class TeacherController {
    /**
     * 老师类业务接口
     */
    @Autowired
    private TeacherService teacherService;

    /**
     * 根据id查询老师信息
     *
     * @param id
     * @return
     */
    @RequestMapping("/queryById")
    public Result queryById(Integer id) throws Exception {

        try {
            //根据父级id查询子级数据
            Teacher teacher = teacherService.queryTeacherById(id);
            //返回结果
            return ResultGenerator.genSuccessResult(teacher);
        } catch (Exception e) {
            e.printStackTrace();
            throw new Exception(e.getMessage());

        }
    }

    /**
     * 添加老师信息
     *
     * @param teacher
     * @return
     */
    @RequestMapping("/insert")
    public Result insertTeacher(Teacher teacher) throws Exception {
        try {
            teacherService.insert(teacher);
            //返回结果
            return ResultGenerator.genSuccessResult();
        } catch (Exception e) {
            e.printStackTrace();
            throw new Exception(e.getMessage());

        }
    }

    /**
     * 修改老师信息
     *
     * @param teacher
     * @return
     */
    @RequestMapping("/update")
    public Result updateTeacher(Teacher teacher) throws Exception {
        try {
            teacherService.update(teacher);
            //返回结果
            return ResultGenerator.genSuccessResult();
        } catch (Exception e) {
            e.printStackTrace();
            throw new Exception(e.getMessage());
        }
    }
    /**
     * 根据id逻辑删除老师信息
     *
     * @param id
     * @return
     */
    @RequestMapping("/delete")
    public Result deleteTeacherById(Integer id) throws Exception{
        try {
            teacherService.deleteById(id);
            //返回结果
            return ResultGenerator.genSuccessResult();
        } catch (Exception e) {
            e.printStackTrace();
            throw new Exception(e.getMessage());
        }
    }
    /**
     * 根据id发布老师信息
     *
     * @param id
     * @return
     */
    @RequestMapping("/release")
    public Result releaseTeacherById(Integer id) throws Exception{
        try {
            teacherService.releaseById(id);
            //返回结果
            return ResultGenerator.genSuccessResult();
        } catch (Exception e) {
            e.printStackTrace();
            throw new Exception(e.getMessage());
        }
    }
    /**
     * 分页查询
     * @param startNumber 当前页
     * @return
     */
    @RequestMapping("/queryPage")
    public Result queryPage(@RequestParam(defaultValue = "1") Integer startNumber, Integer pageSize) throws Exception{
        try {
            //获取封装对象
            Page page = teacherService.queryAll(startNumber,pageSize);
            //返回结果 page.getMenuList() 分页后的数据  page.getTotalRecord()总数
            return ResultGenerator.genSuccessListResult(page.getMenuList(),page.getTotalRecord());
        }catch (Exception e){
            e.printStackTrace();
            throw new Exception(e.getMessage());
        }
    }

    /**
     * 分页查询
     * @param baseId
     * @param startNumber
     * @return
     */
    @RequestMapping("/queryPageByBaseId")
    public Result queryPageByBaseId(Integer baseId , @RequestParam(defaultValue = "1") Integer startNumber,
                                    Integer pageSize) throws Exception{
        try {
            //获取封装对象
            Page page = teacherService.queryByBaseId(baseId, startNumber,pageSize);
            //返回结果 page.getMenuList() 分页后的数据  page.getTotalRecord()总数
            return ResultGenerator.genSuccessListResult(page.getMenuList(),page.getTotalRecord());
        }catch (Exception e){
            e.printStackTrace();
            throw new Exception(e.getMessage());
        }
    }

    /**
     * 根据基地id查询步分页
     * @param baseId
     * @return
     */
    @RequestMapping("/queryByBaseId")
    public Result queryByBaseId(Integer baseId) throws Exception{
        try {
            // 根据基地id查询步分页
            List<Teacher> teacherList = teacherService.queryByBase(baseId);
            //返回结果
            return ResultGenerator.genSuccessResult(teacherList);
        }catch (Exception e){
            e.printStackTrace();
            throw new Exception(e.getMessage());
        }
    }

}
